package RFID;

import java.util.ArrayList;
import java.util.List;

public class MyRFID {

    public String RFIDReader() throws InterruptedException {
        System.setProperty("jna.encoding", "GBK");
        int status;                       //存放返回值
        byte myctrlword = 0;              //读写控制字，一般取0 就行了
        byte afi = 0;                     //卡片应用标识
        byte startblock = 0;              //指定读起始块地址，最大取值255(根据卡类型)
        byte blocknum = 12;               //指定读取块数，最大取值12
        byte[] myuid = new byte[8];       //8字节卡序列号缓冲
        byte[] mydatabuf = new byte[255]; //读卡数据缓冲共255个字节

        status = (int) (MyLibrary.sdtapi.iso15693readex(myctrlword, afi, startblock, blocknum, myuid, mydatabuf) & 0xff);
        String serialnumber = "";
        if (status == 0) {
            MyLibrary.sdtapi.pcdbeep(38);
            for (int i = 0; i < 8; i++) {
                String bytestr = "00" + Integer.toHexString(myuid[i] & 0xff);
                serialnumber = serialnumber + bytestr.substring(bytestr.length() - 2, bytestr.length());
            }
//            log.info("读卡成功，卡序列号：{}" ,serialnumber );
            // 读取卡内数据
           /* for(j = 1; j < mydatabuf[0] + 1; ++j) {
                 bytestr = "00" + Integer.toHexString(mydatabuf[j] & 255);
                 databuf = databuf + bytestr.substring(bytestr.length() - 2, bytestr.length()) + " ";
            }

         System.out.print("卡内数据：" + databuf + "\n"); */
        }
        return serialnumber;
    }

    public List<String> RFIDReadCards() {
        byte flags = 0;
        byte startblock = 22;
        byte blocknum = 0;
        byte[] myuid = new byte[8];
        byte[] revlen = new byte[81];
        byte[] mydatabuf = new byte[1];
        String databuf;
        List<String> list = new ArrayList<>();
//        log.info("\n5-搜寻感应区内多张15693卡\n");
        int status = MyLibrary.sdtapi.iso15693inventory16(startblock, flags, blocknum, myuid, mydatabuf, revlen) & 255;
        if (status == 0) {
            MyLibrary.sdtapi.pcdbeep(38);
            for (int j = 0; j < mydatabuf[0]; j += 9) {
                String serialnumber = "";

                for (int i = 1; i < 9; ++i) {
                    databuf = "00" + Integer.toHexString(revlen[j + i] & 255);
                    serialnumber = serialnumber + databuf.substring(databuf.length() - 2, databuf.length());
                }
                list.add(serialnumber);
//                log.info("卡序列号UID：{}" ,serialnumber );
            }
        }
        return list;
    }


}
